package com.supplychain.scm.core.common.validator.tel;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.Validator;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName PhoneValidationImpl
 * @Description 座机号400800参数校验实现
 * @createTime 11:25:00 2022/04/09
 */
public class Tel400800ValidationImpl implements ConstraintValidator<Tel400800,String> {

    private boolean required = false;

    @Override
    public void initialize(Tel400800 tel400800Validation) {
        required = tel400800Validation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            return  Validator.isMatchRegex(PatternPool.TEL_400_800,value);
        }else{
            if(StringUtils.isBlank(value)){
                return true;
            }else{
                return Validator.isMobile(value);
            }
        }
    }
}
